package chapter9.rent.view;

import chapter9.rent.domain.House;
import chapter9.rent.service.HouseService;
import chapter9.rent.utill.Utility;


public class HouseView {
    private boolean loop = true;//循环控制变量
    private char key = ' ';
    private HouseService houseService = new HouseService(2);

    /*1.显示主菜单
      2.可以退出系统
     */
    public void mainMenu() {

        do {
            System.out.println("-----------房屋出租系统------------------");
            System.out.println("\t\t\t1.新增房源");
            System.out.println("\t\t\t2.查找房屋");
            System.out.println("\t\t\t3.删除房屋信息");
            System.out.println("\t\t\t4.修改房屋信息");
            System.out.println("\t\t\t5.显示房屋信息");
            System.out.println("\t\t\t6.退出");
            System.out.println("请输入你的选择1-6");
            key = Utility.readChar();
            switch (key) {
                case '1':
                    System.out.println("新增");
                    addHouse();
                    break;
                case '2':
                    System.out.println("查找");
                    find();
                    break;
                case '3':
                    System.out.println("删除");
                    delHouse();
                    break;
                case '4':
                    System.out.println("修改");
                    modify();
                    break;
                case '5':
                    listHouse();
                    break;
                case '6':
                    exit();
                    break;
            }

    } while(loop);
}

    //显示房屋列表的界面
    public void listHouse() {
        System.out.println("====房屋列表====");
        System.out.println("编号\t房主\t电话\t地址\t月租\t状态\t");
        //接收数组
        House[] houses = houseService.list();
        for (int i = 0; i < houses.length; i++) {
            if (houses[i] != null) {
                System.out.println(houses[i]);
            }
        }
        System.out.println("======房屋列表显示完毕========");
    }

    //添加房屋的界面,创建House对象，添加到Houses数组
    public void addHouse() {
        System.out.println("====添加房屋======");
        System.out.println("姓名:");
        String name = Utility.readString(10);
        System.out.println("电话号码");
        String phone = Utility.readString(10);
        System.out.println("地址");
        String address = Utility.readString(16);
        System.out.println("月租");
        int rent = Utility.readInt();
        System.out.println("状态");
        String state = Utility.readString(3);
        //创建一个新的House对象，注意Id是系统分配的，用户不能输入
        House newHouse = new House(0, name, phone, address, rent, state);
        if (houseService.add(newHouse)) {
            System.out.println("====房屋添加成功====");
        } else {
            System.out.println("===房屋添加失败====");
        }

    }

    //按照id删除房屋信息，界面
    public void delHouse() {
        System.out.println("===删除房屋信息===");
        System.out.println("请输入要删除的ID，-1表示退出");
        int id = Utility.readInt();//接收输入的ID
        if (id == -1) {
            System.out.println("您已退出删除");
            return;
        }
        char key = Utility.readConfirmSelection();
        if (key == 'Y') {
            if (houseService.delete(id)) {
                System.out.println("您已成功删除id为" + id + "的信息");
            } else {
                System.out.println("删除失败");

            }
        } else {
            System.out.println("放弃了删除");
        }
    }

    public void exit() {
        char choice = Utility.readConfirmSelection();
        if (choice == 'Y') {
            loop = false;
        }
    }

    public void find(){
        System.out.println("===查找房屋信息===");
        System.out.println("请输入要查找的Id");
         int findId = Utility.readInt();
        if (houseService.find(findId)!= null){
            System.out.println("编号\t房主\t电话\t地址\t月租\t状态\t");
            System.out.println(houseService.find(findId));
        }
        else {
            System.out.println("没有该信息");
        }
         }
         public void modify(){
             System.out.println("===修改房屋信息===");
             System.out.println("请输入要修改的房屋Id");
             int id = Utility.readInt();
             House house = houseService.find(id);
             if (house!=null){
                 System.out.println("请输入修改后的name");
                 String name = Utility.readString(10);
                 System.out.println("请输入修改后的phoneNumber");
                 String phone = Utility.readString(10);
                 System.out.println("请输入修改后的adress");
                 String address = Utility.readString(10);
                 System.out.println("请输入修改后的rent");
                 int rent = Utility.readInt();
                 System.out.println("请输入修改后的state");
                 String state = Utility.readString(10);
                 houseService.modify(house,name,phone,address,rent,state);
             }
             else{
                 System.out.println("没有此信息，无法修改");
             }
         }


    }

